package com.ruoyi.hospital.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.hospital.domain.YlDepartment;
import com.ruoyi.hospital.domain.vo.Tree;
import com.ruoyi.hospital.service.IYlDepartmentService;
import com.ruoyi.system.api.domain.SysDept;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 科室信息Controller
 * 
 * @author ruoyi
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/department")
public class YlDepartmentController extends BaseController
{
    @Autowired
    private IYlDepartmentService ylDepartmentService;

    /**
     * 查询科室信息列表
     */
    @RequiresPermissions("hospital:department:list")
    @GetMapping("/list")
    public TableDataInfo list(YlDepartment ylDepartment)
    {
        startPage();
        List<YlDepartment> list = ylDepartmentService.selectYlDepartmentList(ylDepartment);
        return getDataTable(list);
    }

    /**
     * 查询科室级联列表
     */
    @GetMapping("/allList")
    public TableDataInfo treeList()
    {
        List<YlDepartment> list = ylDepartmentService.selectAll();
        Iterator<YlDepartment> iterator = list.iterator();
        while(iterator.hasNext()){
            YlDepartment next = iterator.next();
            if(next.getSuperId()!=0){
                iterator.remove();
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询所有科室列表
     */
    @GetMapping("/allList2")
    public TableDataInfo allList()
    {
        List<YlDepartment> list = ylDepartmentService.selectAll();
        return getDataTable(list);
    }

    /**
     * 导出科室信息列表
     */
    @RequiresPermissions("hospital:department:export")
    @Log(title = "科室信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YlDepartment ylDepartment)
    {
        List<YlDepartment> list = ylDepartmentService.selectYlDepartmentList(ylDepartment);
        ExcelUtil<YlDepartment> util = new ExcelUtil<YlDepartment>(YlDepartment.class);
        util.exportExcel(response, list, "科室信息数据");
    }

    /**
     * 获取科室信息详细信息
     */
    @RequiresPermissions("hospital:department:query")
    @GetMapping(value = "/{departmentId}")
    public AjaxResult getInfo(@PathVariable("departmentId") Long departmentId)
    {
        return AjaxResult.success(ylDepartmentService.selectYlDepartmentByDepartmentId(departmentId));
    }

    /**
     * 获取科室信息详细信息2
     */
    @GetMapping(value = "superListBy/{superId}")
    public TableDataInfo getInfo2(@PathVariable("superId") Long superId)
    {
        List<YlDepartment> list= ylDepartmentService.selectYlDepartmentBySuperID(superId);

        return getDataTable(list);
    }

    /**
     * 新增科室信息
     */
    @RequiresPermissions("hospital:department:add")
    @Log(title = "科室信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YlDepartment ylDepartment)
    {
        return toAjax(ylDepartmentService.insertYlDepartment(ylDepartment));
    }

    /**
     * 修改科室信息
     */
    @RequiresPermissions("hospital:department:edit")
    @Log(title = "科室信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YlDepartment ylDepartment)
    {
        return toAjax(ylDepartmentService.updateYlDepartment(ylDepartment));
    }

    /**
     * 删除科室信息
     */
    @RequiresPermissions("hospital:department:remove")
    @Log(title = "科室信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{departmentIds}")
    public AjaxResult remove(@PathVariable Long[] departmentIds)
    {
        return toAjax(ylDepartmentService.deleteYlDepartmentByDepartmentIds(departmentIds));
    }
}
